import { useEffect } from 'react';

interface Props {
  title?: string;
  onClose: (v?: boolean) => void;
}

export default function RefreshDialog({ title = '温馨提示', onClose }: Props) {
  useEffect(() => {
    window.document.body.style.overflow = 'hidden';
  }, []);

  function removeURLParameter(url: string, parameter: string) {
    const urlObject = new URL(url, window.location.origin);
    urlObject.searchParams.delete(parameter);
    return urlObject.href;
  }

  function handleClose() {
    onClose(true);
    window.document.body.style.overflow = '';
    const newUrl = removeURLParameter(window.location.href, 'code');
    if (newUrl) {
      window.location.href = newUrl;
    }
  }
  return (
    <div className="refresh-dialog d-flex-r ai-center jc-center">
      <div className="refresh-mask" />
      <div className="refresh-center">
        <div className="refresh-title-block">
          <div className="refresh-title">{title}</div>
        </div>
        <div className="refresh-content pl-20 pr-20 pb-10">
          <div className="line-height-30">检测到订单信息已发生变更，请点击刷新获取最新的订单信息!</div>
          <div className="btn mt-10 mb-10 p-10 d-flex-r ai-center jc-center border-radius-8" onClick={handleClose}>
            刷新
          </div>
        </div>
      </div>
    </div>
  );
}
